﻿namespace Hidistro.UI.Web.API
{
    using Hidistro.Entities;
    using Hidistro.Entities.Orders;
    using Hidistro.SaleSystem.Shopping;
    using System;
    using System.Web;

    public class TaobaoOrderHandler : IHttpHandler
    {
        private string GenerateOrderId()
        {
            string str = string.Empty;
            Random random = new Random();
            for (int i = 0; i < 7; i++)
            {
                int num = random.Next();
                str = str + ((char) (0x30 + ((ushort) (num % 10)))).ToString();
            }
            int v = random.Next(0, 9);
            return (DateTime.Now.ToString("yyyyMMdd") + str + v);
        }

        private void ProcessOrderAdd(HttpContext context)
        {
            OrderInfo orderInfo = new OrderInfo {
                OrderId = this.GenerateOrderId(),
                TaobaoOrderId = context.Request.Form["TaobaoOrderId"],
                Remark = context.Request.Form["BuyerMemo"] + context.Request.Form["BuyerMessage"]
            };
            string str = context.Request.Form["SellerFlag"];
            if (!string.IsNullOrEmpty(str) && (str != "0"))
            {
                orderInfo.ManagerMark = new OrderMark?((OrderMark) int.Parse(str));
            }
            orderInfo.ManagerRemark = context.Request.Form["SellerMemo"];
            orderInfo.OrderDate = DateTime.Parse(context.Request.Form["OrderDate"]);
            orderInfo.PayDate = DateTime.Parse(context.Request.Form["PayDate"]);
            orderInfo.UserId = 0x44c;
            orderInfo.RealName = orderInfo.Username = context.Request.Form["Username"];
            orderInfo.EmailAddress = context.Request.Form["EmailAddress"];
            orderInfo.ShipTo = context.Request.Form["ShipTo"];
            orderInfo.ShippingRegion = context.Request.Form["ReceiverState"] + context.Request.Form["ReceiverCity"] + context.Request.Form["ReceiverDistrict"];
            orderInfo.RegionId = RegionHelper.GetRegionId(context.Request.Form["ReceiverDistrict"], context.Request.Form["ReceiverCity"], context.Request.Form["ReceiverState"]);
            orderInfo.Address = context.Request.Form["ReceiverAddress"];
            orderInfo.TelPhone = context.Request.Form["TelPhone"];
            orderInfo.CellPhone = context.Request.Form["CellPhone"];
            orderInfo.ZipCode = context.Request.Form["ZipCode"];
            orderInfo.RealShippingModeId = orderInfo.ShippingModeId = 0;
            orderInfo.RealModeName = orderInfo.ModeName = context.Request.Form["ModeName"];
            orderInfo.PaymentType = "支付宝担宝交易";
            orderInfo.Gateway = "hishop.plugins.payment.alipayassure.assurerequest";
            orderInfo.PayCharge = 0M;
            orderInfo.AdjustedDiscount = 0M;
            string str2 = context.Request.Form["Products"];
            if (string.IsNullOrEmpty(str2))
            {
                context.Response.Write("-1");
            }
            else
            {
                string[] strArray = str2.Split(new char[] { '|' });
                if (strArray.Length <= 0)
                {
                    context.Response.Write("-2");
                }
                else
                {
                    foreach (string str3 in strArray)
                    {
                        string[] strArray2 = str3.Split(new char[] { ',' });
                        LineItemInfo info2 = new LineItemInfo();
                        int result = 0;
                        int.TryParse(strArray2[1], out result);
                        int num2 = 1;
                        int.TryParse(strArray2[3], out num2);
                        info2.SkuId = strArray2[0];
                        info2.ProductId = result;
                        info2.SKU = strArray2[2];
                        info2.Quantity = info2.ShipmentQuantity = num2;
                        info2.ItemCostPrice = info2.ItemAdjustedPrice = decimal.Parse(strArray2[4]);
                        info2.ItemListPrice = decimal.Parse(strArray2[5]);
                        info2.ItemDescription = HttpUtility.UrlDecode(strArray2[6]);
                        info2.ThumbnailsUrl = strArray2[7];
                        info2.ItemWeight = 0M;
                        info2.SKUContent = strArray2[8];
                        info2.PromotionId = 0;
                        info2.PromotionName = "";
                        orderInfo.LineItems.Add(info2.SkuId, info2);
                    }
                    orderInfo.AdjustedFreight = orderInfo.Freight = decimal.Parse(context.Request.Form["PostFee"]);
                    orderInfo.OrderStatus = OrderStatus.BuyerAlreadyPaid;
                    orderInfo.RefundStatus = RefundStatus.None;
                    orderInfo.OrderSource = OrderSource.Taobao;
                    if (ShoppingProcessor.CreatOrder(orderInfo))
                    {
                        context.Response.Write("1");
                    }
                    else
                    {
                        context.Response.Write("0");
                    }
                }
            }
        }

        public void ProcessRequest(HttpContext context)
        {
            string str2;
            context.Response.ContentType = "text/plain";
            GzipExtention.Gzip(context);
            string str = context.Request.Form["action"];
            if (((str2 = str) != null) && (str2 == "OrderAdd"))
            {
                this.ProcessOrderAdd(context);
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

